package com.prp.gwt.client.widgets.admin;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.IListener;
import com.prp.gwt.client.admin.AdminListener;
import com.prp.gwt.client.user.friend.FriendHistoryHandler;
import com.prp.gwt.client.vo.UserList;
import com.prp.gwt.client.widgets.ConfirmDialog;
import com.prp.gwt.client.widgets.IWidget;

public class UserQueryPanel extends VerticalPanel implements IWidget{

	private static UserQueryPanel instance;
	private Grid searchBox = new Grid(2, 3);
	private FlexTable resultTable = new FlexTable();
	private SuggestBox email = new SuggestBox(AdminView.getInstance()
			.getEmailOracle());
	private SuggestBox name = new SuggestBox(AdminView.getInstance()
			.getNameOracle());
	public static UserQueryPanel getInstance() {
		if (instance == null)
			instance = new UserQueryPanel();
		return instance;
	}

	private UserQueryPanel() {
		getEmailQuery();
		getNameQuery();
		add(searchBox);
		resultTable.setCellSpacing(5);
		add(resultTable);
	}

	private void getEmailQuery() {
		searchBox.setHTML(0, 0, "邮箱:");
		searchBox.setWidget(0, 1, email);
		email.setLimit(10);
		email.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				AdminListener.getInstance().getEmailSuggest(email.getText());
				email.showSuggestionList();
			}
		});
		Button search = new Button("精确查找");
		search.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				AdminListener.getInstance().searchForEmail(email.getText());
			}
		});
		searchBox.setWidget(0, 2, search);
	}

	private void getNameQuery() {
		searchBox.setWidget(1, 0, new HTML("用户名/真实姓名:"));
		name.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				AdminListener.getInstance().getNameSuggest(name.getText());
				name.showSuggestionList();
			}
		});
		searchBox.setWidget(1, 1, name);
		Button search = new Button("模糊查找");
		search.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (name.getText().equals("")) {
					update(new UserList());
					return;
				}
				AdminListener.getInstance().searchForName(name.getText());
			}
		});
		searchBox.setWidget(1, 2, search);
	}

	public void reset() {
		int row = resultTable.getRowCount();
		for (int i = row - 1; i >= 0; i--)
			resultTable.removeRow(i);
		email.setText("");
		name.setText("");
	}

	public void update(final UserList userList) {
		int row = resultTable.getRowCount();
		for (int i = row - 1; i >= 0; i--)
			resultTable.removeRow(i);
		if (userList.size() == 0) {
			resultTable.setHTML(0, 0, "没有搜索结果");
			return;
		}
		resultTable.setWidget(0, 0, new Label("邮箱"));
		resultTable.setWidget(0, 1, new Label("用户名"));
		resultTable.setWidget(0, 2, new Label("真实姓名"));
		for (int i = 0; i < userList.size(); i++) {
			int j = i + 1;
			resultTable.setWidget(j, 0, new Label(userList.get(i).getEmail()));
			resultTable.setWidget(j, 1, new Label(userList.get(i).getUsername()));
			resultTable.setWidget(j, 2, new Label(userList.get(i).getRealname()));
//			Button modify = new Button("修改资料");
//			resultTable.setWidget(j, 3, modify);
			Button delete = new Button("删除帐户");
			resultTable.setWidget(j, 3, delete);
			Button friend = new Button("查看好友");
			resultTable.setWidget(j, 4, friend);
			final int k = i;
			delete.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					ConfirmDialog.getInstance().setCallFunction(UserQueryPanel.this, userList.get(k).getEmail());
					ConfirmDialog.getInstance().show(
							"确认删除?",
							"确认删除帐户" + userList.get(k).getEmail() + "吗?<br/>"
									+ "删除后此帐户的所有的信息将无法找回!");
					AdminListener.getInstance().deleteUser(userList.get(k).getEmail());
				}
			});
			friend.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					Window.open(Host.getHOME()+"#"+FriendHistoryHandler.PREFIX_FRIENDLIST+userList.get(k).getId(), "_blank", "");
				}
			});
		}
	}

	@Override
	public void callFunction(Object arg) {
		AdminListener.getInstance().deleteUser((String)arg);
	}

	@Override
	public void setListener(IListener listener) {
		//do nothing
	}

}
